package com.logic.landseaserver.models.latefee.service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.logic.landseaserver.common.enums.BillTypeEnum;
import com.logic.landseaserver.common.util.Arith;
import com.logic.landseaserver.domain.BillDetail;
import com.logic.landseaserver.domain.LateFeeDetail;
import com.logic.landseaserver.persistence.read.BillDetailReadMapper;
import com.logic.landseaserver.persistence.read.LateFeeDetailReadMapper;
import com.logic.landseaserver.persistence.write.LateFeeDetailWriteMapper;
import com.logic.landseaserver.ws.dto.CurrentBillDTO;

/**
 * 
 *
 * 项目名称：system-server</br>
 * 类名称：LateFeeService</br>
 * 类描述：滞纳金相关</br>
 * 创建人：aaron.wang</br>
 * 创建时间：2018年4月12日 上午10:43:23</br>
 * 
 * @version 1.0
 *
 */
@Service
public class LateFeeService
{
    private static final Logger LOG = LoggerFactory.getLogger(LateFeeService.class);
    
    @Autowired
    private LateFeeDetailWriteMapper lateFeeDetailWriteMapper;
    
    @Autowired
    private LateFeeDetailReadMapper lateFeeDetailReadMapper;
    
    @Autowired
    private BillDetailReadMapper billDetailReadMapper;
    
    /**
     * 
     * [简要描述]：</br>
     * [详细描述]：</br>
     * [作者]：aaron.wang(2018-04-12)</br>
     *
     * @param billDto 账单对象
     * @param freeDate 不产生滞纳金的时长
     * @param persentage 交纳滞纳金的百分比
     * @return 滞纳金对象
     *
     */
    public LateFeeDetail prcessLateFee(CurrentBillDTO billDto, Date freeDate, Double persentage)
    {
        LOG.info("prcessLateFee|计算滞纳金开始.BillId=" + billDto.getId() + ",contractId=" + billDto.getContractId());
        
        // 先计算未支付金额
        Double nonePaidMoney = getNonePaidMoney(billDto);
        if (null == nonePaidMoney)
        {
            // 不需要交纳
            return null;
        }
        
        // 开始计算滞纳金
        // 计算缴纳金天数
        //Date start = DateUtil.convertDate(DateUtil.convertDate(freeDate));
        //Date end = DateUtil.convertDate(DateUtil.convertDate(new Date()));
        //Integer minuteDays = DateUtil.differentDaysByMillisecond(start, end);
        
        // 最终滞纳金金额
        Double lateFee = new BigDecimal(nonePaidMoney.doubleValue() * persentage)
            .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        
        // 打印计算方式(原始应付 - 已支付) * 滞纳金百分比  每天累加
        double sourceBillMoney = billDto.getBillMoney().doubleValue();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("(");
        stringBuilder.append(sourceBillMoney);
        stringBuilder.append("-");
        stringBuilder.append(Arith.sub(sourceBillMoney, nonePaidMoney));
        stringBuilder.append(")");
        stringBuilder.append("*");
        stringBuilder.append(persentage);
        
        String cacutlatLog = stringBuilder.toString();
        
        LOG.info("prcessLateFee|本次需要交纳滞纳金" + cacutlatLog + " = " + lateFee);
        
        LateFeeDetail lateFeeDetail = new LateFeeDetail();
        lateFeeDetail.setBillId(billDto.getId());
        lateFeeDetail.setCalculation(cacutlatLog);
        lateFeeDetail.setLateFeeMoney(lateFee);
        lateFeeDetail.initCommonField();
        
        return lateFeeDetail;
    }
    
    /**
     * 
     * [简要描述]：计算账单未付金额</br>
     * [详细描述]：不含滞纳金</br>
     * [作者]：aaron.wang(2018-04-12)</br>
     *
     * @return 交纳的滞纳金
     *
     */
    public Double getNonePaidMoney(CurrentBillDTO billDto)
    {
        // 获取原始应付
        double sourceBillMoney = billDto.getBillMoney().doubleValue();
        
        // 查询账单详情记录
        List<BillDetail> detailList = billDetailReadMapper.getBillDetail(billDto.getId());
        
        // 不需要交纳滞纳金
        if (CollectionUtils.isEmpty(detailList))
        {
            LOG.info("getNonePaidMoney|无账单记录，无需交纳滞纳金.");
            return null;
        }
        
        // 将除了滞纳金之外的所有项已付加在一起,和原始金额相比
        double totalPaidMoney = 0.00d;
        for (BillDetail billDetail : detailList)
        {
            if (BillTypeEnum.CTBI13.getCode() == billDetail.getType())
            {
                continue;
            }
            // 如果详情金额为负的，要直接算应付，而不是已支付
            if (billDetail.getMoney().doubleValue() < 0)
            {
                sourceBillMoney = Arith.add(sourceBillMoney, billDetail.getMoney());
            }
            else
            {
                // 正数金额，计算已支付的钱
                double paidMoney = null == billDetail.getPaidMoney() ? 0.00d : billDetail.getPaidMoney().doubleValue();
                totalPaidMoney = Arith.add(totalPaidMoney, paidMoney);
            }
        }
        
        double nonePaidMoney = Arith.sub(sourceBillMoney, totalPaidMoney);
        
        // 已支付不小于应付，不需要交纳滞纳金
        if (nonePaidMoney <= 0)
        {
            LOG.info("getNonePaidMoney|应付" + sourceBillMoney + ",已付" + totalPaidMoney + ",不需要交纳滞纳金.");
            return null;
        }
        
        LOG.info("getNonePaidMoney|应付" + sourceBillMoney + ",已付" + totalPaidMoney + ",需要交纳滞纳金.");
        return nonePaidMoney;
    }
    
    public void saveLateFeeDetail(LateFeeDetail lateFeeDetail)
    {
        lateFeeDetailWriteMapper.insertSelective(lateFeeDetail);
    }
    
    /**
     * 
     * [简要描述]：查询滞纳金详情列表</br>
     * [详细描述]：</br>
     * [作者]：aaron.wang(2018-04-12)</br>
     *
     * @param billId
     * @return
     *
     */
    public List<LateFeeDetail> latefeeList(Integer billId)
    {
        return lateFeeDetailReadMapper.getListByBillId(billId);
    }
}
